#include <glog/logging.h>

// gtsam
// automatic differentiation expression framework
#include "gtsam/nonlinear/ExpressionFactorGraph.h"
#include "gtsam/slam/expressions.h"

#include "gtsam/geometry/Point2.h"
#include "gtsam/inference/Symbol.h"
#include "gtsam/nonlinear/DoglegOptimizer.h"
#include "gtsam/nonlinear/Values.h"

#include "gtsam_sfm_data.h"

#include <vector>

int main(int argc, char** argv) {
  gtsam::Cal3_S2 K(50.0, 50.0, 0.0, 50.0, 50.0);
  gtsam::noiseModel::Isotropic::shared_ptr measurementNoise =
      gtsam::noiseModel::Isotropic::Sigma(2, 1.0);
  // create the set of ground-truth landmarks and poses
  std::vector<gtsam::Point3> points = createPoints();
  std::vector<gtsam::Pose3> poses = createPoses();

  // create a factor graph
  gtsam::ExpressionFactorGraph graph;

  // specify uncertainty on first pose prior
  gtsam::Vector6 sigmas;
  sigmas << gtsam::Vector3(0.3, 0.3, 0.3), gtsam::Vector3(0.1, 0.1, 0.1);
  gtsam::noiseModel::Diagonal::shared_ptr poseNoise =
      gtsam::noiseModel::Diagonal::Sigmas(sigmas);

  // here we don't use a PriorFactor but directly the ExpressionFactor class
  // x0 is an Expression, and we create a factor wanting it to be equal to
  // poses[0]
  gtsam::Pose3_ x0('x', 0);
  graph.addExpressionFactor(x0, poses[0], poseNoise);

  // we create a constant Expression for the calibration here
  gtsam::Cal3_S2_ cK(K);

  // simulated measurements from each camera pose, adding them to the factor
  // graph
  for (size_t i = 0; i < poses.size(); i++) {
    gtsam::Pose3_ x('x', i);
    gtsam::PinholeCamera<gtsam::Cal3_S2> camera(poses[i], K);
    for (size_t j = 0; j < points.size(); j++) {
      gtsam::Point2 measurement = camera.project(points[j]);
      // below an expression for the prediction of the measurement
      gtsam::Point3_ p('l', j);
      gtsam::Point2_ prediction =
          gtsam::uncalibrate(cK, gtsam::project(gtsam::transformTo(x, p)));
      // again, here we use an ExpressionFactor
      graph.addExpressionFactor(prediction, measurement, measurementNoise);
    }
  }

  // Add prior on first point to constrain scale, again with ExpressionFactor
  gtsam::noiseModel::Isotropic::shared_ptr pointNoise =
      gtsam::noiseModel::Isotropic::Sigma(3, 0.1);
  graph.addExpressionFactor(gtsam::Point3_('l', 0), points[0], pointNoise);

  // create perturbed initial
  gtsam::Values initial;
  gtsam::Pose3 delta(gtsam::Rot3::Rodrigues(-0.1, 0.2, 0.25),
                     gtsam::Point3(0.05, -0.10, 0.20));
  for (size_t i = 0; i < poses.size(); ++i)
    initial.insert(gtsam::Symbol('x', i), poses[i].compose(delta));
  for (size_t j = 0; j < points.size(); ++j)
    initial.insert<gtsam::Point3>(gtsam::Symbol('l', j),
                                  points[j] + gtsam::Point3(-0.25, 0.20, 0.15));
  LOG(INFO) << "initial error = " << graph.error(initial);

  // optimize the graph and print results
  gtsam::Values result = gtsam::DoglegOptimizer(graph, initial).optimize();
  LOG(INFO) << "final error: " << graph.error(result);

  return 0;
}